package com.liuyk.commonlib.router;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.liuyk.commonlib.App;
import com.liuyk.commonlib.router.intercept.CustomNavigationCallback;
import com.liuyk.commonlib.utility.CommonIntentUtils;
import com.liuyk.commonlib.web.RouterConfig;

/**
 * 路由管理
 * <p>
 * Created by liuyakui on 2020/7/1
 */
public class RouterManager {

    public static RouterManager getInstance() {
        return Inner.INSTANCE;
    }

    public Builder builder(String url) {
        return new Builder(url);
    }

    private void jump(String url) {
        jump(null, 0, url, null);
    }

    private void jump(String url, Bundle bundle) {
        jump(null, 0, url, bundle);
    }

    private void jump(Activity context, int requestCode, String url, Bundle bundle) {
        if (TextUtils.isEmpty(url)) {
            return;
        }

        //如果是Scheme形式，需要使用AppRouterHelper.getSchemeUrl()得到地址
        if (url.startsWith(AppRouterHelper.SCHEME_PREFIX)) {
            //表示是一个隐式跳转
            CommonIntentUtils.goToPager(context, requestCode, url.replace(AppRouterHelper.SCHEME_PREFIX, ""), bundle);
            return;
        }

        String router = url;
        if (url.startsWith("http") || url.startsWith("https") || url.startsWith("file:///")) {
            //表示是一个h5链接，打开WebViewActivity
            if (bundle == null) {
                bundle = new Bundle();
            }
            bundle.putString("url", url);
            router = RouterConfig.WEB_ROUTER;
        }

        Postcard postcard = ARouter.getInstance().build(router);
        if (bundle != null && !bundle.isEmpty()) {
            postcard.with(bundle);
        }
        if (context != null) {
            postcard.navigation(context, requestCode, new CustomNavigationCallback());
            return;
        }
        postcard.navigation(App.context, new CustomNavigationCallback());
    }

    public static class Builder {
        private Bundle mBundle;
        private String mUrl;

        public Builder(String url) {
            mBundle = new Bundle();
            mUrl = url;
        }

        public Builder withString(String key, String value) {
            mBundle.putString(key, value);
            return this;
        }

        public Builder withBoolean(String key, boolean value) {
            mBundle.putBoolean(key, value);
            return this;
        }

        public Builder withInt(String key, int value) {
            mBundle.putInt(key, value);
            return this;
        }

        public Builder withLong(String key, long value) {
            mBundle.putLong(key, value);
            return this;
        }

        public Builder withDouble(String key, double value) {
            mBundle.putDouble(key, value);
            return this;
        }

        public Builder bundle(Bundle bundle) {
            mBundle = bundle;
            return this;
        }

        public void jump() {
            RouterManager.getInstance().jump(null, 0, mUrl, mBundle);
        }

        public void jump(Activity activity, int mRequestCode) {
            RouterManager.getInstance().jump(activity, mRequestCode, mUrl, mBundle);
        }
    }

    //依赖注入
    public void inject(Object o) {
        ARouter.getInstance().inject(o);
    }

    private static class Inner {
        private static final RouterManager INSTANCE = new RouterManager();
    }
}
